<?php
namespace app\service;

use think\facade\Db;

class AvatarResourceService
{
    /**
     * 处理头像列表数据，将原始URL替换为OSS URL
     */
    public function processAvatarList($avatarData)
    {
        if (!isset($avatarData['data']) || !is_array($avatarData['data'])) {
            return $avatarData;
        }
        
        // 获取所有头像的OSS映射关系
        $avatarIds = array_column($avatarData['data'], 'aiavatarId');
        $ossMapping = $this->getOssMapping($avatarIds);
        
        // 替换URL
        foreach ($avatarData['data'] as &$avatar) {
            $avatarId = $avatar['aiavatarId'];
            
            if (isset($ossMapping[$avatarId])) {
                $mapping = $ossMapping[$avatarId];
                
                // 替换封面图片URL
                if (!empty($mapping['oss_cover_url'])) {
                    $avatar['coverUrl'] = $mapping['oss_cover_url'];
                }
                
                // 替换预览视频URL
                if (!empty($mapping['oss_preview_url'])) {
                    $avatar['previewVideoUrl'] = $mapping['oss_preview_url'];
                }
            }
        }
        
        return $avatarData;
    }
    
    /**
     * 获取OSS映射关系
     */
    private function getOssMapping($avatarIds)
    {
        if (empty($avatarIds)) {
            return [];
        }
        
        $mappings = Db::table('avatar_resources')
            ->whereIn('avatar_id', $avatarIds)
            ->where('status', 1) // 只获取已成功迁移的
            ->select()
            ->toArray();
            
        $result = [];
        foreach ($mappings as $mapping) {
            $result[$mapping['avatar_id']] = $mapping;
        }
        
        return $result;
    }
    
    /**
     * 检查单个头像是否已迁移
     */
    public function isAvatarMigrated($avatarId)
    {
        $record = Db::table('avatar_resources')
            ->where('avatar_id', $avatarId)
            ->where('status', 1)
            ->find();
            
        return !empty($record);
    }
    
    /**
     * 获取头像OSS URLs
     */
    public function getAvatarOssUrls($avatarId)
    {
        $record = Db::table('avatar_resources')
            ->where('avatar_id', $avatarId)
            ->where('status', 1)
            ->find();
            
        if (empty($record)) {
            return null;
        }
        
        return [
            'coverUrl' => $record['oss_cover_url'],
            'previewVideoUrl' => $record['oss_preview_url']
        ];
    }
    
    /**
     * 获取迁移统计信息
     */
    public function getMigrationStats()
    {
        $stats = Db::table('avatar_resources')
            ->field('status, COUNT(*) as count')
            ->group('status')
            ->select()
            ->toArray();
            
        $result = [
            'total' => 0,
            'migrated' => 0,
            'pending' => 0,
            'failed' => 0
        ];
        
        foreach ($stats as $stat) {
            $result['total'] += $stat['count'];
            
            switch ($stat['status']) {
                case 0:
                    $result['pending'] = $stat['count'];
                    break;
                case 1:
                    $result['migrated'] = $stat['count'];
                    break;
                case 2:
                    $result['failed'] = $stat['count'];
                    break;
            }
        }
        
        return $result;
    }
} 